package org.example.myleet.p483;

public class Solution {
    public String smallestGoodBase(String n) {
        String result = null;
        long num = Long.parseLong(n);
        int mMax = (int) Math.floor(Math.log(num) / Math.log(2));
        for (int m = mMax; m > 1; --m) {
            long k = (long) Math.pow(num, 1.0 / m);
            long sum = 1L, element = 1L;
            for (int i = 0; i < m; ++i) {
                element *= k;
                sum += element;
            }
            if (sum == num) {
                result = String.valueOf(k);
            }
        }
        if (result == null) {
            result = String.valueOf(num - 1);
        }
        return result;
    }
}
